//
//  TopSelectedPicView.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/10.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "TopSelectedPicView.h"
#import "ZLPhotoActionSheet.h"
#import "ZLDefine.h"

@interface TopSelectedPicView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * secrollView;
@property(nonatomic,strong)NSMutableArray * assets;//已选图片数组 <PHAsset>
@property(nonatomic,strong)MCustomButton * addBtn;
@property(nonatomic,strong)UILabel * showPageLabel;
@property(nonatomic,strong)UIView * addView;

@end

@implementation TopSelectedPicView

- (instancetype)initWithFrame:(CGRect)frame {
    self=[super initWithFrame:frame];
    if (self) {
        [self uiConfigure];
    }
    return self;
}
- (void)uiConfigure {
    self.secrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    self.secrollView.pagingEnabled=YES;
    self.secrollView.delegate=self;
    [self addSubview:self.secrollView];
    [self add_addView];
 
     _showPageLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width-45-15, self.height-35, 45, 30)];
    _showPageLabel.font=Font(15);
    _showPageLabel.textAlignment=NSTextAlignmentCenter;
    _showPageLabel.textColor=[UIColor whiteColor];
    _showPageLabel.layer.cornerRadius=10;
    _showPageLabel.font=Font(12);
    _showPageLabel.layer.masksToBounds=YES;
    _showPageLabel.text=@"1/1";
    _showPageLabel.backgroundColor=[MyRGB(153, 153, 153) colorWithAlphaComponent:0.5];
    [self addSubview:_showPageLabel];
    
  
}
- (void)add_addView {
    _addView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.secrollView.width, self.secrollView.height)];
    _addView.backgroundColor=[UIColor whiteColor];
    [self.secrollView addSubview:_addView];
    CGFloat btnWidth = 80;
    _addBtn = [MCustomButton buttonWithType:UIButtonTypeCustom];
    _addBtn.frame=CGRectMake((_addView.width/2)-(btnWidth/2), (_addView.height/2)-(btnWidth/2), btnWidth, btnWidth);
    _addBtn.imageRect=CGRectMake(20, 20, _addBtn.width-40, _addBtn.width-40);
    [_addBtn setImage:[UIImage imageNamed:@"加号.png"] forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    _addBtn.backgroundColor=[UIColor whiteColor];
    
    _addBtn.layer.cornerRadius  = _addBtn.width/2;
    _addBtn.layer.shadowOpacity = 0.5;
    _addBtn.layer.shadowColor   = [UIColor grayColor].CGColor;
    _addBtn.layer.shadowRadius  = 2;
    _addBtn.layer.shadowOffset  = CGSizeMake(0, 2);
    _addBtn.layer.masksToBounds = NO;
    [_addView addSubview:_addBtn];
    
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, _addBtn.bottom+5, _addView.width, 25)];
    label.font=Font(12);
    label.text=@"添加图片(最多添加6张)";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=MyRGB(153, 153, 153);
    [_addView addSubview:label];
    
    
   
}
- (void)refreshUI {
    for (UIView *obj in self.secrollView.subviews) {
        if ([obj isKindOfClass:[UIImageView class]]) {
            [obj removeFromSuperview];
        }
    }
    int i=0;
    for (UIImage *image in self.imageList) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*self.secrollView.width, 0, self.secrollView.width, self.secrollView.height)];
        imageView.backgroundColor=[UIColor colorWithRed: arc4random_uniform(256)/255.0f green: arc4random_uniform(256)/255.0f blue: arc4random_uniform(256)/255.0f alpha:1];
        imageView.image=image;
        imageView.contentMode=UIViewContentModeScaleAspectFill;
        imageView.userInteractionEnabled=YES;
        UIButton * deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        deleteBtn.frame=CGRectMake(10, imageView.height-40, 30, 30);
        [deleteBtn setImage:[UIImage imageNamed:@"btn_del_nor"] forState:UIControlStateNormal];
        deleteBtn.tag=i;
        [deleteBtn addTarget:self action:@selector(deleteBtncilcked:) forControlEvents:UIControlEventTouchUpInside];
        [imageView addSubview:deleteBtn];

        [self.secrollView addSubview:imageView];
        i++;
    }
    _addView.left=self.imageList.count*self.secrollView.width;
    self.secrollView.contentSize=CGSizeMake((self.imageList.count+1)*self.secrollView.width, 0);
    _showPageLabel.text=[NSString stringWithFormat:@"1/%ld",(long)self.imageList.count+1];
}
- (void)deleteBtncilcked:(UIButton *)btn {
    [self.assets removeObjectAtIndex:btn.tag];
    [self.imageList removeObjectAtIndex:btn.tag];
    [self refreshUI];
}
- (void)btnClicked:(UIButton *)btn {
    
    ZLPhotoActionSheet * a = [self getPas];
    [a showPhotoLibrary];
}
- (ZLPhotoActionSheet *)getPas
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    actionSheet.arrSelectedAssets =_assets;
#pragma optional
    //设置照片最大预览数
    actionSheet.maxPreviewCount = 6;
    //设置照片最大选择数
    actionSheet.maxSelectCount = 6;
    //单选模式是否显示选择按钮
    actionSheet.showSelectBtn = NO;
    
#pragma required
    //如果调用的方法没有传sender，则该属性必须提前赋值
    actionSheet.sender = [MPublicManager sharedPublicManager].currentControllr;
    @WeakObj(self);
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        selfWeak.assets=[[NSMutableArray alloc] initWithArray:assets];
        selfWeak.imageList =[[NSMutableArray alloc] init];
        [selfWeak.imageList addObjectsFromArray:images];
//        for (UIImage * image in images) {
//            NSData *data = UIImagePNGRepresentation(image);
//            [selfWeak.imageList addObject:data];
//        }
        [selfWeak refreshUI];
        NSLog(@"image:%@", images);
    }];
    return actionSheet;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
     int page=  scrollView.contentOffset.x/scrollView.width;
    page+=1;
    _showPageLabel.text=[NSString stringWithFormat:@"%ld/%ld",(long)page,(long)self.imageList.count+1];
}
@end
